package com.lhy.service;

import cn.hutool.core.util.IdUtil;
import com.lhy.pojo.Devices;
import com.lhy.pojo.Miner;
import com.lhy.pojo.Repair;
import com.mongodb.client.result.DeleteResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DevicesService {

    @Resource
    private MongoTemplate mongoTemplate;

    //添加设备到mongodb
    public void addDevices(Devices devices){
        //自动生成设备编号
        devices.setDeviceId(IdUtil.getSnowflake(1,1).nextIdStr());
        //记录更新时间
        devices.setUpdateTime(new Date());
        //添加设备信息到mongodb
        mongoTemplate.insert(devices,"devices");
    }

    //更新设备信息
    //追加设备维修记录
    public void updateDeviceAndRepair(Repair repair){
        //获取设备状态
        String deviceStatus=repair.getOperation();
        //更新操作时间
        repair.setOperationTime(new Date());
        Query query = new Query(Criteria.where("deviceId").is(repair.getRepairId()));
        Update update=new Update();
        //更新设备状态
        update.set("deviceStatus",deviceStatus);
        update.push("repair",repair);
        mongoTemplate.upsert(query,update,Devices.class,"devices");
    }

    //通过设备id查询
    public Devices selectDeviceById(String deviceId){
        Query query = new Query(Criteria.where("deviceId").is(deviceId));
        return mongoTemplate.findOne(query,Devices.class,"devices");
    }

    //根据设备编号修改设备信息
    public void updateDevicesInfo(Devices devices){
        Query query = new Query(Criteria.where("deviceId").is(devices.getDeviceId()));
        Boolean exists = mongoTemplate.exists(query, Devices.class, "devices");
        if(exists==false) return;
        Update update=new Update();
        devices.setUpdateTime(new Date());

        if(devices.getDeviceName()!="") update.set("deviceName",devices.getDeviceName());

        if(devices.getDeviceStatus()!="") update.set("deviceStatus",devices.getDeviceStatus());

        if(devices.getPosition()!="") update.set("position",devices.getPosition());

        if(devices.getPurpose()!="") update.set("purpose",devices.getPurpose());

        mongoTemplate.upsert(query,update,Devices.class,"devices");
    }

    //查询所有设备
    public Map<String,Object> selectDevicesList(){
        List<Devices> list = mongoTemplate.findAll(Devices.class,"devices");
        Map<String,Object> result =new HashMap<>();

        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }

    //查询所有在使用的设备
    public Map<String,Object> selectDevicesListOn(){
        List<Devices> list = mongoTemplate.find(new Query(Criteria.where("deviceStatus").is("使用中")),Devices.class,"devices");
        Map<String,Object> result =new HashMap<>();

        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }

    //查询所有未使用的设备
    public Map<String,Object> selectDevicesListOff(){
        Query query=new Query(Criteria.where("deviceStatus").is("待使用"));

        List<Devices> list = mongoTemplate.find(query,Devices.class,"devices");
        Map<String,Object> result =new HashMap<>();

        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }

    public Map<String,Object> selectDevicesListRepair(){
        Query query=new Query(Criteria.where("deviceStatus").is("维修中"));

        List<Devices> list = mongoTemplate.find(query,Devices.class,"devices");
        Map<String,Object> result =new HashMap<>();

        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            result.put("code","0");
            result.put("count",list.size());
            result.put("data",list);
        }

        return result;
    }

    //根据设备编号删除设备
    public boolean deleteDevicesById(String id){
        Query query = new Query(Criteria.where("deviceId").is(id));
        DeleteResult result = mongoTemplate.remove(query, Devices.class, "devices");
        return result.getDeletedCount() >0 ? true : false;
    }
}
